﻿namespace KinkumaInstaller
{
    using System;
    using System.Diagnostics;
    using System.IO;
    using System.Linq;
    using System.Xml.Linq;

    class Program
    {
        public static readonly XNamespace NS = "http://schemas.microsoft.com/developer/msbuild/2003";

        static void Main(string[] args)
        {
            try
            {
                if (args.Length != 2)
                {
                    Console.WriteLine("KinkumaInstaller ソリューションのパス プロジェクトへのパス");
                    return;
                }
                var solutionPath = args[0];
                var projectPath = args[1];
                var libsPath = Path.Combine(solutionPath, "Libs");

                if (!Directory.Exists(libsPath))
                {
                    Directory.CreateDirectory(libsPath);
                }
                if (!File.Exists(projectPath))
                {
                    Console.WriteLine(projectPath + "が見つかりません");
                    Console.ReadLine();
                    return;
                }

                Utils.DownloadKinkuma(libsPath);
                var refs = Utils.References(libsPath);

                var project = XElement.Load(projectPath);
                var itemGroup = project.Elements(NS + "ItemGroup").FirstOrDefault();
                if (itemGroup == null)
                {
                    Console.WriteLine("ItemGroupがみつかりませんでした");
                    return;
                }
                itemGroup.Add(refs);

                project.Save(projectPath);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }

    static class Utils
    {
        // ソリューションフォルダの下のLibsからdllを探す。Silverlightとデザイン時のものは排除
        public static XElement[] References(string libsPath)
        {
            var files = Directory.GetFiles(libsPath, @"*.dll", SearchOption.AllDirectories);
            return files.Select(f => f.Replace(libsPath, ""))
                .Select(f => f.Split('\\'))
                .Where(f =>
                {
                    var dir = f[f.Length - 2];
                    if (dir.StartsWith("SL"))
                    {
                        return false;
                    }
                    if (dir == "Design")
                    {
                        return false;
                    }
                    return true;
                })
                .Select(f => Path.Combine(f))
                .Select(f =>
                    new XElement(Program.NS + "Reference",
                        new XAttribute("Include", Path.GetFileNameWithoutExtension(f)),
                        new XElement(Program.NS + "SpecificVersion", "False"),
                        new XElement(Program.NS + "HintPath", Path.Combine(@"..\", "Libs", f)))
                ).ToArray();
        }

        public static void DownloadKinkuma(string libsPath)
        {
            var info = new ProcessStartInfo("nuget.exe", "install KinkumaFramework");
            info.WorkingDirectory = libsPath;
            if (!Directory.Exists(info.WorkingDirectory))
            {
                Directory.CreateDirectory(info.WorkingDirectory);
            }
            var p = Process.Start(info);
            p.WaitForExit();
        }
    }
}
